import csv
import mysql.connector

def read_csv_file(file_path):
    arr=[]
    with open(file_path, mode='r', encoding='GBK') as csvfile:
        csv_reader = csv.reader(csvfile)
        # print(csv_reader)
        for row in csv_reader:
            arr.append([row[0]+'#'+row[20],row[4],row[21]])
    return arr

datas=read_csv_file('D:/Desk/快麦光板-0216.csv')
print(len(datas))
# 数据位置查询
# print(ord('U') - ord('A'),ord('V') - ord('A'))

mydb = mysql.connector.connect(
  host="192.168.10.81",
  user="root",
  passwd="",
  database="xiaocheng"
)

mycursor = mydb.cursor()
mycursor.execute("truncate table data_kuaimai_guangban") 
mydb.commit()
print(mycursor.rowcount, "记录删除成功。")

sql = "insert into data_kuaimai_guangban (style_name,km_number,size) values (%s,%s,%s)" 
mycursor.executemany(sql, datas) 
mydb.commit()    # 数据表内容有更新，必须使用到该语句
 
print(mycursor.rowcount, "记录插入成功。")

mycursor.close()
mydb.close()